package com.leyou.item.controller;

import com.leyou.item.pojo.Specification;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    /**
     * 查询商品分类对应的规格参数
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<String> querySpecificationByCategoryId(@PathVariable("id") Long id){
        Specification spec = specificationService.queryById(id);
        if (spec==null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(spec.getSpecifications());
    }

    /**
     * 新增
     * @param specification
     */
    @PostMapping
    public ResponseEntity<Void> addSpec(Specification specification) {
        specificationService.addSpec(specification);
        return ResponseEntity.status(HttpStatus.CREATED).build();
//        return ResponseEntity.ok().build();
    }

    /**
     * 更新
     * @param specification
     */
    @PutMapping
    public ResponseEntity<Void>  updateSpec(Specification specification) {
        specificationService.updateSpec(specification);
        return ResponseEntity.status(HttpStatus.ACCEPTED).build();
//        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Void>  deleteSpec(@PathVariable("id")Long id) {
        Specification spec = new Specification();
        spec.setCategoryId(id);
        specificationService.deleteSpec(spec);
        return ResponseEntity.ok().build();
    }

}
